/**************************************************************
 *   File:	Application.cpp
 * Author:	Andrew Thorpe
 *  Brief:	The main application class
 **************************************************************/
#include "StdAfx.h"
#include "Application.h"
#include "Window.h"
#include "Renderer.h"
#include "ScreenQuad.h"
#include "TextureManager.h"

using namespace Fossil;
Application::Application(void)
: m_pWindow(0)
{
	Run();
}

Application::~Application(void)
{
}

void Application::Run()
{
	Initialise();
	RunLoop();
	Shutdown();
}

void Application::Initialise()
{
	new Log;
	Log::Instance()->Initialise(LogLevelTrivial);

	LOG_TRIVIAL("Trivial");
	LOG_WARNING("Warning");
	LOG_ERROR("Error");

	new TextureManager;
	new Renderer;

	m_pWindow = Window::Create("Fossil", 20, 40, 1024, 768, 32, false);

	TextureManager::Instance()->Initialise();
	Renderer::Instance()->Initialise();

	m_pScreenQuad = new ScreenQuad();
	m_pScreenQuad->SetSize(0.5f, 0.3f);
}

void Application::RunLoop()
{
	MSG msg;
	bool done = false;
	while(!done)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				done = true;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			Renderer::Instance()->Render(m_pScreenQuad);
			SwapBuffers(m_pWindow->GetHDC());
		}
	}
}

void Application::Shutdown()
{
	Renderer::Instance()->Destroy();
	TextureManager::Instance()->Destroy();
	m_pWindow->Destroy();

	Log::Instance()->Destroy();
}
